package com.ship.dispatch.controller;

import com.ship.common.core.web.domain.JsonResult;
import com.ship.common.log.annotation.Log;
import com.ship.common.log.enums.BusinessType;
import com.ship.dispatch.bean.fob.SpBpmCoordinate;
import com.ship.dispatch.bpm.service.ProcessInstanceService;
import com.ship.dispatch.bpm.service.TaskService;
import com.ship.dispatch.bpm.vo.BpmStartVO;
import com.ship.dispatch.bpm.vo.BpmTaskVo;
import com.ship.dispatch.service.fob.SpBpmCoordinateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 *@title BpmFactoryController
 *@description 审批流程操作接口
 *@author yl
 *@create 2023/12/13 0013
 */
@Api(value = "bpm", tags = "流程服务启动执行类")
@RestController
@RequestMapping("/bpm")
public class BpmFactoryController {
    @Autowired
    private ProcessInstanceService processInstanceService;
    @Autowired
    private TaskService taskService;
    @Autowired
    SpBpmCoordinateService spBpmCoordinateService;

    /**
     *  开启FOB 审批流程实例
     * @param bpmStartVO
     * @return
     */
    @PostMapping("/createFOBProcess")
    @Log(title = "开启FOB审批流程实例",businessType = BusinessType.OTHER)
    public JsonResult<String> createFOBProcessInstance(@RequestBody @Validated BpmStartVO bpmStartVO){
        return processInstanceService.createProcessInstance(bpmStartVO);
    }

    @ApiOperation(value = "商务执行流程启动")
    @PostMapping("/createSubleassProcess")
    @Log(title = "开启FOB审批流程实例",businessType = BusinessType.OTHER)
    public JsonResult<String> createSubleassProcessInstance(@RequestBody @Validated BpmStartVO bpmStartVO){
        return processInstanceService.createSubleassProcessInstance(bpmStartVO);
    }

    /**
     * 获取流程定义全部任务信息
     * @param processInstaneId
     * @return
     */
    @GetMapping("/process/alltasks/{processInstaneId}")
    public JsonResult getProcessAllTasks(@PathVariable("processInstaneId") String processInstaneId){
        return taskService.getProcessAllTasks(processInstaneId);
    }


    @GetMapping("/process/tasks/{processInstaneId}")
    public JsonResult getProcessTasks(@PathVariable("processInstaneId") String processInstaneId){
        return taskService.flowRecord(processInstaneId);
    }

    /**
     * 获取当前登录用户待办任务列表
     * @param processInstaneId
     * @return
     */
    @GetMapping("/todoList/{processInstaneId}")
    public JsonResult todoProcessInstanceList(@PathVariable("processInstaneId") String processInstaneId){
        return taskService.todoProcessInstanceList(processInstaneId);
    }

    /**
     * 处理任务
     * @param taskVo
     * @return
     */
    @PostMapping("/approval/task")
    @Log(title = "处理任务",businessType = BusinessType.OTHER)
    public JsonResult approvalTask(@RequestBody BpmTaskVo taskVo){
        return taskService.complete(taskVo);
    }

    /**
     * 维护更新流程节点坐标
     * @param spBpmCoordinate
     * @return
     */
    @PostMapping("/update/xy")
    @Log(title = "维护更新流程节点坐标",businessType = BusinessType.UPDATE)
    public JsonResult updateXY(@RequestBody SpBpmCoordinate spBpmCoordinate){
        return spBpmCoordinateService.updateXY(spBpmCoordinate);
    }

    /**
     * 取消流程实例
     * @param processInstaneId
     * @return
     */
    @GetMapping("/cancel/{processInstaneId}")
    @Log(title = "取消流程实例",businessType = BusinessType.DELETE)
    public JsonResult cacenProcessInstance(@PathVariable("processInstaneId") String processInstaneId){
        return  processInstanceService.cacenProcessInstance(processInstaneId);
    }
}
